算法思想: 把作业时间从大到小排序,选择未安排cpu的最长作业到累积时间最短的一个cpu上,直至所有作业都被安排完。
使用堆排序,降低时间复杂度,用数组建立堆,cpu堆(小根堆)的每个节点指向一个作业链表。作业堆为大根堆。
#include <iostream>
#include <string>
using namespace std;
struct Node{
string name;
int length;
Node * next;
};
struct cpu{
string name;
int hight;
Node * first;
};
cpu * p=NULL;int psize=0;
Node * q=NULL;int qsize=0;
void siftP(int start,int end)
{
//小跟堆的筛选函数
int t=(start+1)*2-1;
while(t<=end)
{
int temp=start;
if(p[temp].hight>p[t].hight)
temp=t;
if(t+1<=end&&p[temp].hight>p[t+1].hight)
temp=t+1;
if(temp==start)
break;
else
{
cpu CPU=p[start